package com.Long.mq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Title:
 * @Description:
 * @Author: guowl
 * @version： 1.0
 * @Date:2021/7/23
 * @Copyright: Copyright(c)2021 RedaFlight.com All Rights Reserved
 */
public class mqconsumer {
    public static final String EXCAHANG_NAME = "FANOUT_EXCHANAGE";
    public static final String USER_NAME = "admin";
    public static final String PASSWORD = "admin";
    public static final String HOST = "47.101.154.152";
    public static final int PORT = 5672;
    public static final String QUEUW_NAME1 = "queue1";
    public static final String QUEUW_NAME2 = "queue2";

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);
        factory.setUsername(USER_NAME);
        factory.setPassword(PASSWORD);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.basicConsume(QUEUW_NAME1,new DefaultConsumer(channel){
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            System.out.println("consumer 消费了消息："+new String(body));
            channel.basicAck(envelope.getDeliveryTag(),false);//告诉mq 消费了消息 消息确认机制（确认之后就会删除消息 顺序消费）
        }
    });
    }
}
